package com.xiaolong.log.config;

import com.xiaolong.log.aspectj.ErrorLogAspect;
import com.xiaolong.log.aspectj.LogAspect;
import com.xiaolong.log.send.RocketMqSendHandler;
import com.xiaolong.log.send.SendHandler;
import com.xiaolong.log.util.SendUtil;
import org.apache.rocketmq.spring.autoconfigure.RocketMQAutoConfiguration;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;

@Configuration
@ConditionalOnProperty(prefix = "buding.log", name = "enable", havingValue = "true", matchIfMissing = false)
@EnableConfigurationProperties(LogProperties.class)
@Import({RocketMqSendLogConfiguration.class,LogAspectsConfiguration.class})
@AutoConfigureAfter(value = RocketMQAutoConfiguration.class)
public class LogAutoConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(LogAutoConfiguration.class);

    @Bean
    @ConditionalOnMissingBean(SendHandler.class)
    public InitializationChecker initializationChecker() {
        return new InitializationChecker();
    }

    public static class InitializationChecker {
        @EventListener(ContextRefreshedEvent.class)
        public void checkInitialization() {
            logger.error("日志模块初始化失败，日志组件将不会生效，请检查您的配置。");
        }
    }

}